<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Contacto extends CI_Controller {
	
	public function __construct()
	{
		parent::__construct();
		$this->load->library('session');
		$this->load->library('form_validation');
		$this->load->library('email');
		$this->load->helper('url');
		$this->load->helper('form');
	}
	
	public function index()
	{
		show_404();
	}
	
	public function captcha()
	{
		$this->load->helper('captcha');
		
		$word = array_merge(range(0,9), range('a','z'), range('A', 'Z'));
		$word = implode('', $word);
		$word = substr(str_shuffle($word), 0, 4);
		$captcha = create_captcha(array(
			'word'=>$word,
			'img_path'=>'./assets/captcha/',
			'img_url'=>base_url().'assets/captcha/',
			'img_width'=>200,
			'img_height'=>32,
			'font_path'=>'./assets/font/2.ttf',
			'expiration'=>10
		));
		
		$this->session->set_userdata(array('captcha_miembros'=>$captcha['word']));
		echo $captcha['image'];		
	}
	
	public function newsletter()
	{								
		// This method will have the credentials validation
		$this->form_validation->set_rules('T1', 'T1', 'trim|required|xss_clean|valid_email');
		
		if($this->form_validation->run())
		{
			$input = $this->input->post();
			$this->_send_email_newsletter($input);
			echo '<p> <strong style="color:#fff;">
			Gracias, te has inscrito a nuestro newsletter.</strong></p>';
		}
	}
	
	public function miembros()
	{								
		// This method will have the credentials validation
		$this->form_validation->set_rules('name', 'Name', 'trim|required|xss_clean');
		$this->form_validation->set_rules('msg', 'Msg', 'trim|required|xss_clean');
		$this->form_validation->set_rules('email', 'Email', 'trim|required|xss_clean|valid_email');
		$this->form_validation->set_rules('captcha', 'Captcha', 'callback_validate_captcha');
		
		if($this->form_validation->run())
		{
			$input = $this->input->post();
			$this->_send_email_miembros($input);
			$this->session->unset_userdata('captcha_miembros');
			echo '<p><strong>Tu mensaje ha sido enviado exitosamente.<br>Gracias por escribirnos.</strong></p>';
		}
	}
	
	public function validate_captcha($captcha){
		if($captcha === $this->session->userdata['captcha_miembros'])
		{
			return TRUE;
		}
		else
		{
			$this->form_validation->set_message('validate_captcha', 'El código captcha es incorrecto');
			return FALSE;
		}
	}
	
	private function _send_email_newsletter($input)
	{		
		// initialize email
		$this->email->initialize(array('mailtype'=>'html'));
		
		// email admin		
		$this->email->clear();
		$this->email->from('noreply@starscamp.net', 'Starscamp');
		$this->email->to('info@starscamp.net');
		
		$this->email->subject('Starscamp - Newsletter');
		$this->email->message('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
		<html xmlns="http://www.w3.org/1999/xhtml">
		<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>Formulario de contacto</title>
		</head>
		
		<body>
			  <table align=center width=100% style="padding:20px;" >
		<tr><td>
		<table align=center width=560 style="font-family:sans-serif; color:#FFF; background-color:#335259;">         
		<tr><td colspan=3 height=20></td></tr>        
        <tr>
        	<td width=20></td>
			<td><p><img src="http://starscamp.net/img/logo.png" alt=Starscamp></p>
		<h2 style="color:#fff;">Newsletter</h2>
			<p style="color:#fff;">
				Una persona se ha suscrito al Newsletter de Starscamp.			
			</p>
			<p><strong>Email:<strong> <a style="color:#FFF"; href="mailto:'.$input['T1'].'">'.$input['T1'].'</a></p>	
            </td>
            <td width=20></td>
       </tr>       
       <tr><td colspan=3 height=20></td></tr>        
		</table>	
		</td></tr>
		</table>
		</body>
		<html>');
		
		$this->email->send();
		
		// email cliente
		$this->email->clear();
		$this->email->from('info@starscamp.net', 'Starscamp');
		$this->email->to($input['T1']);
		
		$this->email->subject('Starscamp - Newsletter');
		$this->email->message('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
		<html xmlns="http://www.w3.org/1999/xhtml">
		<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>Formulario de contacto</title>
		</head>
		
		<body>
			            <table align=center width=100% style="padding:20px;" >
		<tr><td>
		<table align=center width=560 style="font-family:sans-serif; color:#FFF; background-color:#335259;">         
		<tr><td colspan=3 height=20></td></tr>        
        <tr>
        	<td width=20></td>
			<td><p><img src="http://starscamp.net/img/logo.png" alt=Starscamp></p>
			
			<p style="color:#fff;">
				Gracias por escribirnos, muy pronto
				nos pondremos en contacto contigo
				para responderte.
			</p>
			<p style="color:#fff;">
				General Trinidad Morán 990 - Lince. Lima-Perú.<br>
				T/ (01) 624 1608
			</p>
            </td>
            <td width=20></td>
       </tr>       
       <tr><td colspan=3 height=20></td></tr>        
		</table>	
		</td></tr>
		</table>
		</body>
		<html>');
		
		$this->email->send();
	}
	
	private function _send_email_miembros($input)
	{		
		// initialize email
		$this->email->initialize(array('mailtype'=>'html'));
		
		// email admin		
		$this->email->clear();
		$this->email->from('noreply@starscamp.net', 'Starscamp');
		$this->email->to('info@starscamp.net');
		
		$this->email->subject('Starscamp - Hazte Miembro('.$input['name'].')');
		$this->email->message('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
		<html xmlns="http://www.w3.org/1999/xhtml">
		<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>Formulario de contacto</title>
		</head>
		<body>
		<table align=center width=100% style="padding:20px;" >
		<tr><td>
		<table align=center width=560 style="font-family:sans-serif; color:#FFF; background-color:#335259;">         
		<tr><td colspan=3 height=20></td></tr>        
        <tr>
        	<td width=20></td>
			<td><p><img src="http://starscamp.net/img/logo.png" alt=Starscamp></p>
			
			<h2 style="color:#fff;">Feedback</h2>
			
			<p><strong style="color:#fff;">Nombre:</strong> '.$input['name'].'</p>
			
			<p><strong style="color:#fff;">Email:<strong> <a style="color:#FFF"; href="mailto:'.$input['email'].'">'.$input['email'].'</a></p>
			
			<p><strong style="color:#fff;">Mensaje:</strong></p>
			
			<p style="color:#fff;">'.$input['msg'].'</p></td>
            <td width=20></td>
       </tr>       
       <tr><td colspan=3 height=20></td></tr>        
		</table>	
		</td></tr>
		</table>
		</body>
		<html>');
		
		$this->email->send();
		
		// email cliente
		$this->email->clear();
		$this->email->from('info@starscamp.net', 'Starscamp');
		$this->email->to($input['email']);
		
		$this->email->subject('Starscamp - Hazte Miembro');
		$this->email->message('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
		<html xmlns="http://www.w3.org/1999/xhtml">
		<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>Formulario de contacto</title>
		</head>
		
		<body>
			            <table align=center width=100% style="padding:20px;" >
		<tr><td>
		<table align=center width=560 style="font-family:sans-serif; color:#FFF; background-color:#335259;">         
		<tr><td colspan=3 height=20></td></tr>        
        <tr>
        	<td width=20></td>
			<td><p><img src="http://starscamp.net/img/logo.png" alt=Starscamp></p>
			
			<h2 style="color:#fff;">Hola '.$input['name'].'</h2>
			<p style="color:#fff;">
				Gracias por escribirnos, muy pronto
				nos pondremos en contacto contigo
				para responderte.
			</p>
			<p style="color:#fff;">
				General Trinidad Morán 990 - Lince. Lima-Perú.<br>
				T/ (01) 624 1608
			</p>
            </td>
            <td width=20></td>
       </tr>       
       <tr><td colspan=3 height=20></td></tr>        
		</table>	
		</td></tr>
		</table>
		</body>
		<html>');
		
		$this->email->send();
	}
}

/* End of file home.php */
/* Location: ./application/controllers/home.php */